import os
import importlib.util




def get_func(path, func_name):
    '''
    function: 根据路径获取文件中的类方法
    :param path:
    :param func_name:
    :return:
    '''
    if not os.path.isfile(path):
        raise IOError
    # 根据提供的目标文件路径，返回模块的说明  spec_from_file_location(module_name, file_path)
    module_desc = importlib.util.spec_from_file_location("_", path)
    # 根据传入的模块说明返回引入的模块 module_from_spec（模块说明）
    moudle_spec = importlib.util.module_from_spec(module_desc)
    # exec_module执行加载方法
    module_desc.loader.exec_module(moudle_spec)
    if hasattr(moudle_spec, func_name):
        return getattr(moudle_spec, func_name)


class selectFunc:

    def __init__(self,operator_name,class_name=''):
        '''
        function: 模糊查找算子类方法
        :param operator_name: 算子名
        :param class_name: 类名
        '''
        pass


if __name__=="__main__":
    # 查询当前目录
    # root_path = os.path.realpath(os.path.dirname(os.path.dirname(__file__)))  # 项目的根目录
    # project_path = os.path.join(root_path, 'OperationOperators')  # 项目所在目录
    # temp_path = os.path.join(project_path, 'Operators')  # 目标文件上级目录
    # temp2_path = os.path.join(temp_path, 'Operators')  # 目标文件上级目录
    # file_a_path = os.path.join(temp_path, 'PowerAverageOperator.py')  # 目标文件目录

    # 直接赋予地址
    file_a_path = '/Operators/FunctionOperators/PowerAverageOperator.py'

    func = get_func(file_a_path, "AA")

    # 用()执行方法
    func()

    list1 = [([0.31, 0.24], [0.73, 0.72]), ([0.97, 0.12], [0.12, 0.05]),
             ([0.8, 0.52], [0.73, 0.15]), ([0.91, 0.49], [0.42, 0.47]), ([0.95, 0.06], [0.19, 0.1])]
    Pa = func(list1)
    print(Pa.getResult())                    # 测试 getResult √







